package com.yunkaweilai.android.activity.operation.integral;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.DialogPayInterface;
import com.yunkaweilai.android.interfaces.UpdateIntegralNumInterfaces;
import com.yunkaweilai.android.model.CheckMemberSendBean;
import com.yunkaweilai.android.model.CheckSendBean;
import com.yunkaweilai.android.model.integral.IntegralCreateOrderModel;
import com.yunkaweilai.android.model.integral.IntegralCreateShopModel;
import com.yunkaweilai.android.model.integral.IntegralGoodListModel;
import com.yunkaweilai.android.model.integral.IntegralSearchModel;
import com.yunkaweilai.android.model.integral.IntegralShopListModel;
import com.yunkaweilai.android.model.integral.IntegralTypeListModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.UpdateIntegralNumDialog;
import com.yunkaweilai.android.view.dialog.integral.IntegralRightDialog;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 积分换礼
 * Created by Administrator on 2017/8/1.
 */

public class IntegralExchangeActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate, DialogPayInterface, UpdateIntegralNumInterfaces {

    @BindView(R.id.id_edt_search)
    EditText idEdtSearch;
    @BindView(R.id.id_img_clean)
    ImageView idImgClean;
    @BindView(R.id.id_listView_left)
    ListView idListViewLeft;
    @BindView(R.id.id_listView_right)
    ListView idListViewRight;
    @BindView(R.id.id_shop_llayout)
    LinearLayout idShopLlayout;
    @BindView(R.id.id_listView_search)
    ListView idListViewSearch;
    @BindView(R.id.id_search_rllayout)
    RelativeLayout idSearchRllayout;
    @BindView(R.id.content_view)
    FrameLayout contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.id_multipleStatusView1)
    MultipleStatusView idMultipleStatusView1;
    @BindView(R.id.id_tv_all_price)
    TextView idTvAllPrice;
    @BindView(R.id.id_tv_checkout)
    TextView idTvCheckout;
    public static final String INTEGRAL_MEMBER_ID = "INTEGRAL_MEMBER_ID";
    public static final String INTEGRAL_MEMBER_POINTS = "INTEGRAL_MEMBER_POINTS";
    public static final String INTEGRAL_MEMBER_CARD = "INTEGRAL_MEMBER_CARD";
    @BindView(R.id.id_refresh)
    BGARefreshLayout idRefresh;
    private String member_id = "";
    private int member_points = 0;
    private int selected_points = 0;
    private String gift_category_id = "0";
    private Gson gson = new Gson();
    private int mpage = 1;
    private boolean mHasMore = true;
    private IntegralCreateShopModel integralCreateShopModel;
    private Map<String, IntegralGoodListModel> shopMap = new HashMap<>();
    private ArrayList<IntegralTypeListModel.DataBean.ListBean> typeList = new ArrayList<>();
    private CommonAdapter<IntegralTypeListModel.DataBean.ListBean> typeAdapter;
    private ArrayList<IntegralGoodListModel> shopList = new ArrayList<>();
    private CommonAdapter<IntegralGoodListModel> shopAdapter;
    private String keywords = "";
    private CommonAdapter<IntegralGoodListModel> searchAdapter;
    private ArrayList<IntegralGoodListModel> searchList = new ArrayList<>();
    private String card_no = "";
    private boolean sms = false;
    private boolean wechat = false;
    private boolean print = false;

    public static void actionStart(Context context, String member_id, int member_points, String Card_no) {
        Intent intent = new Intent(context, IntegralExchangeActivity.class);
        intent.putExtra(INTEGRAL_MEMBER_ID, member_id);
        intent.putExtra(INTEGRAL_MEMBER_POINTS, member_points);
        intent.putExtra(INTEGRAL_MEMBER_CARD, Card_no);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_exchange);
        new TitleBuilder(mAc).setTitleText("积分兑换").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ("".equals(idEdtSearch.getText().toString())) {
                            finish();
                        } else {
                            idImgClean.setVisibility(View.GONE);
                            idEdtSearch.setText("");
                        }
                    }
                });
        ButterKnife.bind(this);
        initRefreshLayout(idRefresh, this, true);
        member_id = getIntent().getStringExtra(INTEGRAL_MEMBER_ID);
        member_points = getIntent().getIntExtra(INTEGRAL_MEMBER_POINTS, 0);
        card_no = getIntent().getStringExtra(INTEGRAL_MEMBER_CARD);
        initGetTuisong2();

        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initAdapter();
        initClick();
        initHttp();
        refreshCart();
    }

    private void initGetTuisong2() {
        HttpRequest.postUrl(Api.CHECK_SEND)
                .addParams("view", "points")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkcode = Utils.checkCode(mAc,json);
                        if (checkcode) {
                            Gson gson = new Gson();
                            CheckSendBean checkSendBean = gson.fromJson(json, CheckSendBean.class);
                            if (checkSendBean.getData().getIs_check_print().equals("1")) {
                                print = true;
                            } else {
                                print = false;
                            }
                            if (checkSendBean.getData().getIs_send_sms().equals("1")) {
                                selectMember(card_no, true);
                            } else {
                                selectMember(card_no, false);
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    private void refreshCart() {
        idTvAllPrice.setText(Html.fromHtml(selected_points + "积分" + "<font color='#999999'> (剩余" + member_points + "积分)</font>"));
    }

    private void initClick() {
        idEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null && idEdtSearch.length() > 0) {
                    //点击搜索要做的操作
                    keywords = idEdtSearch.getText().toString();
                    InputMethodUtils.closeSoftKeyboard(mAc);
                    initSearch();
                }
                return false;
            }
        });

        idEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s == null || s.length() == 0) {
                    idImgClean.setVisibility(View.GONE);
                    idSearchRllayout.setVisibility(View.GONE);
                    idShopLlayout.setVisibility(View.VISIBLE);
                    return;
                } else {
                    idImgClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        idListViewLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                gift_category_id = typeList.get(i).getGift_category_id();
                idRefresh.beginRefreshing();
                initShopListHttp(true);
                for (int j = 0; j < typeList.size(); j++) {
                    IntegralTypeListModel.DataBean.ListBean listBean = typeList.get(j);
                    if (j == i) {
                        listBean.setCheck(true);
                    } else {
                        listBean.setCheck(false);
                    }
                }
                typeAdapter.notifyDataSetChanged();
            }
        });

        idTvCheckout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idTvCheckout.setClickable(false);
                getSelectedList();
            }
        });

        idImgClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idImgClean.setVisibility(View.GONE);
                idEdtSearch.setText("");
            }
        });

    }

    private void initAdapter() {
        typeAdapter = new CommonAdapter<IntegralTypeListModel.DataBean.ListBean>(mAc, R.layout.item_list_left, typeList) {
            @Override
            protected void convert(ViewHolder viewHolder, IntegralTypeListModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_textView, item.getGift_category_name() + "");
                TextView idTvBuyNum = viewHolder.getView(R.id.id_tv_buy_num);

                TextView idTv = viewHolder.getView(R.id.id_textView);
                RelativeLayout rlayout = viewHolder.getView(R.id.id_rlayout_all);
                if (item.isCheck()) {
                    rlayout.setBackgroundColor(getResources().getColor(R.color.bg_white));
                    idTv.setTextColor(getResources().getColor(R.color.wjx_content_txt));
                    TextPaint tp = idTv.getPaint();
                    tp.setFakeBoldText(true);//选中加粗
                } else {
                    rlayout.setBackgroundColor(getResources().getColor(R.color.wjx_type_list_bg));
                    idTv.setTextColor(getResources().getColor(R.color.wjx_content_txt_black_right));
                    TextPaint tp = idTv.getPaint();
                    tp.setFakeBoldText(false);//选中加粗
                }
                int num = 0;
                for (String key : shopMap.keySet()) {
                    IntegralGoodListModel listBean = shopMap.get(key);
                    if (listBean.getCategory_id().equals(item.getGift_category_id())) {
                        num += listBean.getBuy_num();
                    }
                }
                if (num > 0) {
                    idTvBuyNum.setVisibility(View.VISIBLE);
                    idTvBuyNum.setText(num + "");
                } else {
                    idTvBuyNum.setVisibility(View.GONE);
                    idTvBuyNum.setText("0");
                }
            }
        };
        shopAdapter = new CommonAdapter<IntegralGoodListModel>(mAc, R.layout.item_list_shop_consumption, shopList) {
            @Override
            protected void convert(ViewHolder viewHolder, final IntegralGoodListModel item, int position) {
                ImageView mIdImg = viewHolder.getView(R.id.id_img);
                Utils.shopInfoImg(mAc, item.getGift_image(), mIdImg);
//                mIdImg.setVisibility(View.GONE);
                viewHolder.setText(R.id.id_tv_name, item.getGift_name() + "");
                viewHolder.setText(R.id.id_tv_price, item.getPoints_price() + "");
                viewHolder.setText(R.id.id_tv_unit, "/积分");
//                viewHolder.setText(R.id.id_tv_info, "编号：" + item.getGift_sku() + " 库存：" + item.getGift_stock());
                viewHolder.setText(R.id.id_tv_info, "库存：" + item.getGift_stock());
                double size = 0;
                for (String key : shopMap.keySet()) {
                    IntegralGoodListModel listBean = shopMap.get(key);
                    if (listBean.getId().equals(item.getId())) {
                        size = size + listBean.getBuy_num();
                    }
                }
                final ImageView idImgAdd = viewHolder.getView(R.id.id_img_add);
                ImageView idImgDel = viewHolder.getView(R.id.id_img_del);
                TextView idTvAddNum = viewHolder.getView(R.id.id_tv_add_num);
                if (size > 0) {
                    idImgDel.setVisibility(View.VISIBLE);
                    idTvAddNum.setText((int) size + "");
                    idTvAddNum.setVisibility(View.VISIBLE);
                } else {
                    idImgDel.setVisibility(View.GONE);
                    idTvAddNum.setText("0");
                    idTvAddNum.setVisibility(View.GONE);
                }
                idTvAddNum.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UpdateIntegralNumDialog dialog = new UpdateIntegralNumDialog(mAc, item.getId(), IntegralExchangeActivity.this, item.getGift_stock(), "修改数量");
                        dialog.setCanceledOnTouchOutside(false);
                        dialog.show();
                    }
                });

                //增加
                idImgAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntegralGoodListModel listBean = shopMap.get(item.getId());
                        if (listBean == null) {
                            int nums = 1;
                            selected_points += item.getPoints_price();
                            if (nums > item.getGift_stock()) {
                                showToast("库存不足");
                            } else {
                                if (selected_points > member_points) {
                                    selected_points -= item.getPoints_price();
                                    showToast("积分不足");
                                } else {
                                    item.setBuy_num(nums);
                                    shopMap.put(item.getId(), item);
                                }
                            }
                        } else {
                            if (item.getGift_stock() > listBean.getBuy_num()) {
                                selected_points += item.getPoints_price();
                                if (selected_points > member_points) {
                                    selected_points -= item.getPoints_price();
                                    showToast("积分不足");
                                } else {
                                    listBean.setBuy_num(listBean.getBuy_num() + 1);
                                }
                            } else {
                                showToast("库存不足");
                            }
                        }
                        refreshCart();
                        shopAdapter.notifyDataSetChanged();
                        typeAdapter.notifyDataSetChanged();
                        searchAdapter.notifyDataSetChanged();
                    }
                });

                //减少
                idImgDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntegralGoodListModel listBean = shopMap.get(item.getId());
                        if (listBean == null) {
                            int nums = item.getBuy_num() - 1;
                            item.setBuy_num(nums <= 0 ? 0 : nums);
                            if (nums > 0) {
                                selected_points -= item.getPoints_price();
                                shopMap.put(item.getId(), item);
                            }
                        } else {
                            int buy_num = listBean.getBuy_num();
                            selected_points -= item.getPoints_price();
                            if (buy_num - 1 > 0) {
                                listBean.setBuy_num(buy_num - 1);
                            } else {
                                shopMap.remove(listBean.getId());
                            }
                        }
                        refreshCart();
                        shopAdapter.notifyDataSetChanged();
                        typeAdapter.notifyDataSetChanged();
                        searchAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        idListViewLeft.setAdapter(typeAdapter);
        idListViewRight.setAdapter(shopAdapter);

        searchAdapter = new CommonAdapter<IntegralGoodListModel>(mAc, R.layout.item_list_shop_consumption, searchList) {
            @Override
            protected void convert(ViewHolder viewHolder, final IntegralGoodListModel item, int position) {
                ImageView mIdImg = viewHolder.getView(R.id.id_img);
                Utils.shopInfoImg(mAc, item.getGift_image(), mIdImg);
//                mIdImg.setVisibility(View.GONE);
                viewHolder.setText(R.id.id_tv_name, item.getGift_name() + "");
                viewHolder.setText(R.id.id_tv_price, item.getPoints_price() + "");
                viewHolder.setText(R.id.id_tv_unit, "/积分");
//                viewHolder.setText(R.id.id_tv_info, "编号：" + item.getGift_sku() + " 库存：" + item.getGift_stock());
                viewHolder.setText(R.id.id_tv_info, "库存：" + item.getGift_stock());
                double size = 0;
                for (String key : shopMap.keySet()) {
                    IntegralGoodListModel listBean = shopMap.get(key);
                    if (listBean.getId().equals(item.getId())) {
                        size = size + listBean.getBuy_num();
                    }
                }
                final ImageView idImgAdd = viewHolder.getView(R.id.id_img_add);
                ImageView idImgDel = viewHolder.getView(R.id.id_img_del);
                TextView idTvAddNum = viewHolder.getView(R.id.id_tv_add_num);
                if (size > 0) {
                    idImgDel.setVisibility(View.VISIBLE);
                    idTvAddNum.setText((int) size + "");
                    idTvAddNum.setVisibility(View.VISIBLE);
                } else {
                    idImgDel.setVisibility(View.GONE);
                    idTvAddNum.setText("0");
                    idTvAddNum.setVisibility(View.GONE);
                }
                idTvAddNum.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UpdateIntegralNumDialog dialog = new UpdateIntegralNumDialog(mAc, item.getId(), IntegralExchangeActivity.this, item.getGift_stock(), "修改数量");
                        dialog.setCanceledOnTouchOutside(false);
                        dialog.show();
                    }
                });

                //增加
                idImgAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntegralGoodListModel listBean = shopMap.get(item.getId());
                        if (listBean == null) {
                            int nums = 1;
                            selected_points += item.getPoints_price();
                            if (nums > item.getGift_stock()) {
                                showToast("库存不足");
                            } else {
                                if (selected_points > member_points) {
                                    selected_points -= item.getPoints_price();
                                    showToast("积分不足");
                                } else {
                                    item.setBuy_num(nums);
                                    shopMap.put(item.getId(), item);
                                }
                            }
                        } else {
                            if (item.getGift_stock() > listBean.getBuy_num()) {
                                selected_points += item.getPoints_price();
                                if (selected_points > member_points) {
                                    selected_points -= item.getPoints_price();
                                    showToast("积分不足");
                                } else {
                                    listBean.setBuy_num(listBean.getBuy_num() + 1);
                                }
                            } else {
                                showToast("库存不足");
                            }
                        }
                        refreshCart();
                        shopAdapter.notifyDataSetChanged();
                        typeAdapter.notifyDataSetChanged();
                        searchAdapter.notifyDataSetChanged();
                    }
                });

                //减少
                idImgDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntegralGoodListModel listBean = shopMap.get(item.getId());
                        if (listBean == null) {
                            int nums = item.getBuy_num() - 1;
                            item.setBuy_num(nums <= 0 ? 0 : nums);
                            if (nums > 0) {
                                selected_points -= item.getPoints_price();
                                shopMap.put(item.getId(), item);
                            }
                        } else {
                            int buy_num = listBean.getBuy_num();
                            selected_points -= item.getPoints_price();
                            if (buy_num - 1 > 0) {
                                listBean.setBuy_num(buy_num - 1);
                            } else {
                                shopMap.remove(listBean.getId());
                            }
                        }
                        refreshCart();
                        shopAdapter.notifyDataSetChanged();
                        typeAdapter.notifyDataSetChanged();
                        searchAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        idListViewSearch.setAdapter(searchAdapter);
    }


    private void initSearch() {
        HttpRequest.postUrl(Api.CONSUME_SCAN_GIFT)
                .addParams("keyword", keywords)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            IntegralSearchModel searchModel = gson.fromJson(json, IntegralSearchModel.class);
                            searchList.clear();
                            searchList.addAll(searchModel.getData().getGoodsList());
                            searchAdapter.notifyDataSetChanged();
                            if (searchList.size() > 0) {
                                idShopLlayout.setVisibility(View.GONE);
                                idSearchRllayout.setVisibility(View.VISIBLE);
                            } else {
                                showToast("暂无此商品");
                            }

                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


    private void selectMember(String card_no, final boolean open_sms) {
        HttpRequest.postUrl(Api.CONSUME_MEMBER)
                .addParams("keyword", card_no)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkcode = Utils.checkCode(mAc,json);
                        if (checkcode) {
                            Gson gson = new Gson();
                            CheckMemberSendBean checkMemberSendBean = gson.fromJson(json, CheckMemberSendBean.class);
                            if (!StringUtils.isEmpty(checkMemberSendBean.getData().getMemberInfo().getMember_tel()) && open_sms) {
                                sms = true;
                            } else {
                                sms = false;
                            }
                            if (checkMemberSendBean.getData().getMemberInfo().getIs_push_wechat() == 1) {
                                wechat = true;
                            } else {
                                wechat = false;
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    //获取分类
    private void initHttp() {
        HttpRequest.postUrl(Api.APP_INTEGRAL_TYPE)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            IntegralTypeListModel integralTypeModel = gson.fromJson(json, IntegralTypeListModel.class);
                            typeList.clear();
                            typeList.addAll(integralTypeModel.getData().getList());
                            typeAdapter.notifyDataSetChanged();
                            if (typeList.size() > 0) {
                                typeList.get(0).setCheck(true);
                                gift_category_id = typeList.get(0).getGift_category_id();
                                initShopListHttp(true);
                            } else {
                                if (idMultipleStatusView != null) {
                                    idMultipleStatusView.showEmpty();
                                }
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });

    }

    //获取商品列表
    private boolean initShopListHttp(boolean refresh) {
        mpage = refresh ? 1 : mpage;
        if ((!mHasMore) && (!refresh)) {
            idRefresh.endLoadingMore();
            showToast("没有更多数据了");
            return false;
        }
        HttpRequest.postUrl(Api.CONSUME_FIGT_SHOP_CATEGORY)
                .addParams("ShowPage", "1")
                .addParams("PageSize", "10")
                .addParams("is_status", "1")
                .addParams("CurrPage", mpage + "")
                .addParams("gift_category_id", gift_category_id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        idRefresh.endRefreshing();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            IntegralShopListModel integralShopListModel = gson.fromJson(json, IntegralShopListModel.class);
                            shopList.clear();
                            shopList.addAll(integralShopListModel.getData().getList());
                            mHasMore = integralShopListModel.getData().getPage().isNext();
                            shopAdapter.notifyDataSetChanged();
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                                idMultipleStatusView1.showContent();
                            }
                            if (shopList.isEmpty()) {
                                idMultipleStatusView1.showEmpty();
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        idRefresh.endRefreshing();
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });


        return true;


    }

    //创建选择商品
    private void getSelectedList() {
        String str = "";
        Iterator<String> iter = shopMap.keySet().iterator();
        String key = null;
        for (; iter.hasNext(); ) {
            key = iter.next();
            IntegralGoodListModel listBean = shopMap.get(key);
            if (listBean.getBuy_num() == 0.0 || listBean.getBuy_num() == 0) {
                iter.remove();
            } else {
                str = str + listBean.getId() + "|" + listBean.getBuy_num() + ",";
            }
        }

        HttpRequest.postUrl(Api.GIFT_SELECTED_LIST_SHOP)
                .addParams("member_id", member_id)
                .addParams("SelectedGiftStr", str)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        idTvCheckout.setClickable(true);
                        boolean checkCode = Utils.checkCode(mAc,json);
                        if (checkCode) {
                            integralCreateShopModel = gson.fromJson(json, IntegralCreateShopModel.class);
                            for (int i = 0; i < integralCreateShopModel.getData().getGiftList().size(); i++) {
                                IntegralCreateShopModel.DataBean.GiftListBean giftListBean = integralCreateShopModel.getData().getGiftList().get(i);
                                int size = 0;
                                for (String key : shopMap.keySet()) {
                                    IntegralGoodListModel listBean = shopMap.get(key);
                                    if (listBean.getId().equals(giftListBean.getGift_id())) {
                                        listBean.setBuy_num(giftListBean.getGift_num());
                                        size++;
                                    }
                                }
                                if (size == 0) {
                                    IntegralGoodListModel shopModel = createShopModel(giftListBean.getGift_num(), giftListBean.getGift_name(), giftListBean.getGift_id(), giftListBean.getPoints_price()
                                            , giftListBean.getGift_sku(), giftListBean.getGift_stock());
                                    shopMap.put(giftListBean.getGift_id(), shopModel);
                                }
                                int points = 0;
                                for (String key : shopMap.keySet()) {
                                    IntegralGoodListModel listBean = shopMap.get(key);
                                    points += listBean.getPoints_price() * listBean.getBuy_num();
                                }
                                selected_points = points;
                                refreshCart();
                                typeAdapter.notifyDataSetChanged();
                                shopAdapter.notifyDataSetChanged();
                            }
                            showRightDialog();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        idTvCheckout.setClickable(true);
                    }
                });
    }

    //弹出确认对话框
    private void showRightDialog() {
        if (shopMap.size() == 0) {
            showToast("还未选中礼品");
            return;
        }
        IntegralRightDialog dialog = new IntegralRightDialog(mAc, this, member_points + "", selected_points + "", shopMap, sms, wechat, print);
        dialog.show();

    }


    //创建商品model
    private IntegralGoodListModel createShopModel(int buynums, String name, String id, int points, String sku, int stock) {
        IntegralGoodListModel model = new IntegralGoodListModel();
        model.setBuy_num(buynums);
        model.setGift_name(name);
        model.setId(id);
        model.setPoints_price(points);
        model.setGift_sku(sku);
        model.setGift_stock(stock);
        return model;
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        initShopListHttp(true);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return initShopListHttp(false);
    }

    //创建订单
    @Override
    public void payMoney() {
        showProgressDialog("请稍等，正在创建订单");
        HttpRequest.postUrl(Api.CREATE_INTEGRAL_ORDER)
                .addParams("member_id", member_id)
                .addParams("orderInfo[SelectedGiftStr]", integralCreateShopModel.getData().getOrderInfo().getSelectedGiftStr())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            showToast("兑换成功");
                            IntegralCreateOrderModel integralCreateOrderModel = gson.fromJson(json, IntegralCreateOrderModel.class);
                            IntegralExchangeSuccessActivity.actionStart(mAc, integralCreateOrderModel.getData().getTid(), integralCreateOrderModel.getData().getInfo().getConsume_time());
                            finish();
                        }
                        dismissProgressDialog();
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                    }
                });
    }

    @Override
    public void updateNum(String sku, double nums) {
        double buy_num = shopMap.get(sku).getBuy_num();
        shopMap.get(sku).setBuy_num((int) nums);
        double points = 0;
        for (IntegralGoodListModel model : shopMap.values()) {
            points = points + model.getPoints_price() * model.getBuy_num();
        }
        if (points <= member_points) {
            selected_points = (int) points;
            refreshCart();
            shopAdapter.notifyDataSetChanged();
            typeAdapter.notifyDataSetChanged();
            searchAdapter.notifyDataSetChanged();
        } else {
            shopMap.get(sku).setBuy_num((int) buy_num);
            showToast("会员积分不足");
        }
    }
}
